package ovm.gui;

import com.vaadin.ui.HorizontalLayout;
import ovm.gui.menu.MainMenu;
import ovm.gui.tools.ToolsPanel;
import ovm.gui.views.AbstractView;
import ovm.gui.views.DefaultView;
import ovm.gui.views.news.NewsView;

import java.util.LinkedList;

public class Navigator extends AbstractWindow {

    private int currentUser;

    private HorizontalLayout mainMenuLayout;
    private HorizontalLayout toolsPanelLayout;
    private HorizontalLayout mainViewLayout;

    private MainMenu mainMenu;
    private ToolsPanel toolsPanel;
    private AbstractView mainView;

    private LinkedList<AbstractView> viewHistory;

    private int currentIndex;
    private int currentSize;

    public Navigator(int user) {
        super();
        currentUser = user;
    }

    @Override
    public void draw() {
        setCaption("Основное приложение");
        removeAllComponents();

        mainMenuLayout = new HorizontalLayout();
        mainMenuLayout.setSizeFull();
        toolsPanelLayout = new HorizontalLayout();
        toolsPanelLayout.setSizeFull();
        mainViewLayout = new HorizontalLayout();
        mainViewLayout.setSizeFull();
        addComponent(mainMenuLayout);
        addComponent(toolsPanelLayout);
        addComponent(mainViewLayout);

        viewHistory = new LinkedList<AbstractView>();

        setMainMenu(new MainMenu());
        setToolsPanel(new ToolsPanel());
        setMainView(new NewsView());
    }

    public int getCurrentUser() {
        return currentUser;
    }

    public void setMainView (AbstractView view) {
        if (view == null) {
            showNotification("Произошла какая-то ошибка");
            return;
        }
        mainViewLayout.removeAllComponents();
        this.mainView = view;
        mainViewLayout.addComponent(view);
        if (viewHistory.size() == 0) {
            viewHistory.add(view);
            currentIndex = 0;
            currentSize = 0;
        } else {
            if (currentIndex + 1 < viewHistory.size()) {
                viewHistory.set(currentIndex + 1, view);
            } else {
                viewHistory.add(view);
            }
            currentIndex++;
            currentSize = currentIndex;
        }
        toolsPanel.setNextButtonStatus(hasNextView());
        toolsPanel.setPreviousButtonState(hasPreviousView());
    }

    public void setMainMenu (MainMenu mainMenu) {
        mainMenuLayout.removeAllComponents();
        this.mainMenu = mainMenu;
        mainMenuLayout.addComponent(mainMenu);
    }

    public void setToolsPanel (ToolsPanel toolsPanel) {
        toolsPanelLayout.removeAllComponents();
        this.toolsPanel = toolsPanel;
        toolsPanelLayout.addComponent(toolsPanel);
    }

    public void goNextView () {
        currentIndex++;
        mainViewLayout.removeAllComponents();
        mainViewLayout.addComponent(viewHistory.get(currentIndex));
        toolsPanel.setNextButtonStatus(hasNextView());
        toolsPanel.setPreviousButtonState(hasPreviousView());
    }

    public void goPreviousView () {
        currentIndex--;
        mainViewLayout.removeAllComponents();
        mainViewLayout.addComponent(viewHistory.get(currentIndex));
        toolsPanel.setNextButtonStatus(hasNextView());
        toolsPanel.setPreviousButtonState(hasPreviousView());
    }

    private boolean hasNextView () {
        return currentIndex < currentSize;
    }

    private boolean hasPreviousView () {
        return currentIndex > 0;
    }

}
